package com.sky.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.sky.constant.MessageConstant;
import com.sky.po.StoredRecordPO;
import com.sky.result.Result;
import com.sky.service.StoredRecordService;
import com.sky.utils.AliOssUtil;
import com.sky.utils.TxCosUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Content: 通用接口
 * @Author: Asen
 * @Date: 2023/8/31 16:29
 */
@Slf4j
@Api(tags = "通用接口")
@RestController
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private TxCosUtil txCosUtil;
    @Autowired
    private StoredRecordService storedRecordService;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        StoredRecordPO result = null;
        try {
//            imageUrl = aliOssUtil.upload(file.getBytes(), newName);
            result = txCosUtil.upload(file);
        } catch (Exception e) {
            log.error("文件上传失败：{}",e);
        }
        if(ObjectUtil.isNotNull(result)){
            storedRecordService.insertRecord(result);
            return Result.success(result.getAbsolutePath());
        }
       return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
